var subpages = ['examples/default.html', 'examples/market.html', 'examples/live.html', 'examples/news.html', 'examples/setting.html', 'examples/sub.html'];
// 创建加载内容窗口
var topoffset = '0px';

var subpage_style = {
	top: topoffset,
	bottom: '51px',
	scrollIndicator: 'none'
};

var aniShow = {};
//当前激活选项
var activeTab = subpages[0];

//mui初始化
mui.init();

mui.plusReady(function() {
	if(!HasNetWorkConnection()) {
		ShowMsg('当前无网络，请检查网络设置。', '温馨提示');
		//outSet('当前无网络，请检查网络设置。');
	}
	
	
	// 设置系统状态栏样式为深色底白字
	setInterval(function() { 
		setStatusBarStyle();

	}, 200);
	// 
	var self = plus.webview.currentWebview();

	for(var i = 0; i < subpages.length; i++) {
		var temp = {};
		var sub = plus.webview.create(subpages[i], subpages[i], subpage_style);

		if(i <= 2) {
			//实现每个频道下拉刷新功能
			/*sub.setPullToRefresh({
				support: true,
				height: '50px',
				range: '100px',
				style: 'circle',
				offset: '1px'
			}, pulldownRefresh);*/
			sub.setPullToRefresh({
				support: true,
				height: "50px",
				range: "200px",
				contentdown: {
					caption: "下拉可以刷新"
				},
				contentover: {
					caption: "释放立即刷新"
				},
				contentrefresh: {
					caption: "正在刷新..."
				}
			}, pulldownRefresh);
		}

		if(i > 0) {
			sub.hide();
		} else {
			temp[subpages[i]] = "true";
			mui.extend(aniShow, temp);

		}
		self.append(sub);
	}
	setTimeout(function() {
		plus.webview.getWebviewById(subpages[0]).evalJS('toShowTheFirst()');
	}, 500);

	//备份mui.back，mui.back已将窗口关闭逻辑封装的比较完善（预加载及父子窗口），因此最好复用mui.back
	/*var old_back = mui.back;
	mui.back = function() {
		var main = plus.android.runtimeMainActivity();
		main.moveTaskToBack(false);
	}*/

	mui('#bar a').each(function(index, item) {
		item.addEventListener('tap', function() {

			var fa = item.querySelector("use").getAttribute("yx");

			item.querySelector("use").setAttribute("xlink:href", fa);
			var ss = $(this).siblings("a").find("use");

			$.each(ss, function(i, d) {
				var fas = d.getAttribute("wx");
				d.setAttribute("xlink:href", fas);
			});

			var targetTab = subpages[index];
			if(targetTab == activeTab) {
				return;
			}

			changeView(index);

			//更改当前活跃的选项卡
			activeTab = targetTab;

		});
	});

	/*//自定义事件，模拟点击“首页选项卡”
	document.addEventListener('gohome', function() {
		var defaultTab = document.getElementById("defaultTab");
		//模拟首页点击
		mui.trigger(defaultTab, 'tap');
		//切换选项卡高亮
		var current = document.querySelector(".mui-bar-tab>.mui-tab-item.mui-active");
		if(defaultTab !== current) {
			current.classList.remove('mui-active');
			defaultTab.classList.add('mui-active');
		}
	});*/

});

/**
 * 下拉刷新具体业务实现
 */
function pulldownRefresh() {
	setTimeout(function() {
		plus.webview.getWebviewById(activeTab).reload();
		plus.webview.getWebviewById(activeTab).endPullToRefresh();
	}, 1500);
}

function changeView(index) {
	//console.log(index);
	plus.webview.getWebviewById(subpages[index]).show();

}

function tapBar(index) {

	mui.trigger(document.getElementById('bar').children[index], 'tap');

}

/*消息推送 并打开对应的板块详情*/
// 监听plusready事件  
document.addEventListener("plusready", function() {

	// 监听点击消息事件
	plus.push.addEventListener("click", function(msg) {

		getPushInfo();
		getMsg(msg);
		return;
	}, false);
	// 监听在线消息事件
	plus.push.addEventListener("receive", function(msg) {
		plus.push.createMessage(msg.content, msg); //创建一条系统消息
		//outSet(JSON.stringify(msg));
		//getMsg(msg);
		return;

		if(msg.aps) { // Apple APNS message
			outSet("接收到在线APNS消息：");
		} else {
			outSet("接收到在线透传消息：");
		}
		logoutPushMsg(msg);
	}, false);
	//获取客户端cid
	function getPushInfo() {
		var info = plus.push.getClientInfo();
		outSet("收到消息通知：" + info.clientid);
		outLine("token: " + info.token);
		outLine("clientid: " + info.clientid);
		outLine("appid: " + info.appid);
		outLine("appkey: " + info.appkey);
	}

	function ShowMsg() {
		mui.confirm('收到消息通知', '消息通知', ['查看', '取消'], function(e) {
			if(e.index == 0) {

			} else {

			}

		});
	}

	function getMsg(msg) {
		// 判断是从本地创建还是离线推送的消息
		switch(msg.payload) {
			case "LocalMSG":
				outSet("点击本地创建消息启动：");
				break;
			default:
				outLine(msg.payload);
				var payload = JSON.parse(msg.payload);
				if(payload.type == '文章明细') {
					openWindowWithTitle('examples/newsneiy.html?id=' + payload.url, payload.title);
				} else if(payload.type == '行情明细') { //行情详情
					openWindowWithTitle('examples/marketView.html?code=' + payload.url, payload.title);
				} else if(payload.type == '行情') { //行情列表
					plus.webview.getLaunchWebview().show();
					setTimeout(function() {

						plus.webview.getLaunchWebview().evalJS('changeView(1);');
					}, 500);
				} else if(payload.type == '直播') { //行情列表
					plus.webview.getLaunchWebview().show();
					setTimeout(function() {

						plus.webview.getLaunchWebview().evalJS('changeView(2);');
					}, 500);
				} else if(payload.type == '资讯') { //行情列表
					plus.webview.getLaunchWebview().show();
					setTimeout(function() {

						plus.webview.getLaunchWebview().evalJS('changeView(3);');
					}, 500);
				} else { //其他   ps: 一律当外部网页打开
					openWindowWithTitle(payload.url, payload.title);
				}
				break;
		}
	}
}, false);

function setStatusBarBackground(color) {
	// 设置系统状态栏背景色为红色
	plus.navigator.setStatusBarBackground(color);

}

function setStatusBarStyle() {
	var top = plus.webview.getTopWebview();

	if(activeTab == subpages[0] && top.getURL().indexOf('index.html') > -1) {

		if(plus.navigator.getStatusBarBackground() != '#FFFFFF') {
			plus.navigator.setStatusBarStyle('dark');
			setStatusBarBackground('#FFFFFF');
		}

	} else {

		if(plus.navigator.getStatusBarBackground() != '#313131') {
			plus.navigator.setStatusBarStyle('light');
			setStatusBarBackground('#313131');
		}

	}

} 

 